﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;

namespace iResume
{
    public partial class createAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CompareValidator2.ValueToCompare = DateTime.Today.ToShortDateString();            
            if (!IsPostBack)
            {
                SetCaptchaText();
                captcha.Text = (string)Session["Captcha"];
            }
            fullnametxb.Focus();
        
        }    

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (tandcckb.Checked == true)
            {
                createadminbtn.Enabled = true;
                createadminbtn.Focus();
            }
            else
            {

                createadminbtn.Enabled = false;
            }

        }

        private void SetCaptchaText()
        {
            Random oRandom = new Random();
            int iNumber = oRandom.Next(100000, 999999);
            Session["Captcha"] = iNumber.ToString();
        }

        bool ReturnValue()
        {
            return false;
        }

        protected void createadminbtn_Click(object sender, EventArgs e)
        {

            if (captchatxb.Text != captcha.Text)
            {
                Panel5.Visible = true;

            }
            else
            {
                Panel5.Visible = false;

                ArrayList validateAEmail = new ArrayList();
                validateAEmail = DBManager.retrieveAEmail();

                foreach (string mail in validateAEmail)
                {
                    if (mail == emailtxb.Text)
                    {
                        lblerror.Visible = true;
                    }

                }
                if (lblerror.Visible == false)
                {


                    string fullname = fullnametxb.Text;
                    string email = emailtxb.Text;
                    string password = passtxb.Text;
                    DateTime dateofbirth = DateTime.ParseExact(dobtxbadmin.Text, "dd/MM/yyyy", null);

                    if (dateofbirth < DateTime.Today)
                    {
                        CompareValidator2.Text = "";
                        CompareValidator2.Text = "Invaild date. No future is allowed.";
                    }

                    string gender = "";
                    string address = addtxb.Text;
                    string postalcode = postaltxb.Text;
                    string contact = contxb.Text;
                    string nationality = nationalityddb.SelectedItem.Text;

                    if (genderrd.SelectedValue != string.Empty)
                    {
                        gender = genderrd.SelectedValue;
                    }

                    string HashedPassword = "";



                    //This is to hash the new password user have
                    HashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(passtxb.Text, "sha1");
                    passtxb.Text = HashedPassword;

                    fullnametxb.Text = "";
                    emailtxb.Text = "";
                    passtxb.Text = "";
                    dobtxbadmin.Text = "";
                    addtxb.Text = "";
                    postaltxb.Text = "";
                    contxb.Text = "";


                    Admin a = new Admin(email, fullname, HashedPassword, dateofbirth, gender, address, postalcode, nationality, contact);
                    DBManager myDBManager = new DBManager();
                    DBManager.createAdmin(a);
                    Response.Redirect("createAdminAcctSuccess.aspx");
                }

            }
        }
        

        protected void emailtxb_TextChanged(object sender, EventArgs e)
        {

        }
    }
}